import Cat2Doc from "./modules/index.js";
import * as Cat2DocCss from './style.css'

let c2d;
window.onload = function () {
    const parentElement = document.getElementById('c2d-doc');
    c2d = new Cat2Doc(parentElement, {
        lazy: true
    });
    // 默认加载说明文件
    c2d.importFormUrl('README.md').then(()=>{
        console.log('The import was successful');
    });

    // 点击导入文档
    const buttonImport = document.getElementById('import-c2d');
    buttonImport.addEventListener('click', function () {
        // 动态创建一个文件类型的input
        const inpEle = document.createElement("input");
        inpEle.id = `__file_${Math.trunc(Math.random() * 100000)}`;
        inpEle.type = "file";
        inpEle.style.display = "none";
        // 文件类型限制
        inpEle.accept = '.docx,.md,.c2d';
        // 多选限制
        inpEle.multiple = false;
        inpEle.addEventListener("change", event => {
            c2d.import(event.target.files[0]).then(()=>{
                console.log('The import was successful');
                inpEle.remove();
            });
        }, {once: true});
        document.body.appendChild(inpEle);
        // 点击选中文件
        inpEle.click();
    });

    // 点击导出DOCX
    const buttonExportDocx = document.getElementById('export-docx');
    buttonExportDocx.addEventListener('click', function () {
        c2d.export('文档示例.docx').then(()=>{
            alert('The export was successful');
        }).catch(()=>alert('Failed to export Docx !'));
    });

    // 点击导出PDF
    const buttonExportPdf = document.getElementById('export-pdf');
    buttonExportPdf.addEventListener('click', function () {
        c2d.export('文档示例.pdf').then(()=>{
            alert('The export was successful');
        }).catch(()=>alert('Failed to export PDF !'));
    });

    // 点击导出MD
    const buttonExportMd = document.getElementById('export-markdown');
    buttonExportMd.addEventListener('click', function () {
        c2d.export('文档示例.md').then(()=>{
            alert('The export was successful');
        }).catch(()=>alert('Failed to export MD !'));
    });

    // 点击导出C2D
    const buttonExportC2d = document.getElementById('export-c2d');
    buttonExportC2d.addEventListener('click', function () {
        c2d.export('文档示例.c2d').then(()=>{
            alert('The export was successful');
        }).catch(()=>alert('Failed to export C2D !'));
    });
}
window.onunload = function () {
    c2d && c2d.destory();
}

console.log('欢迎使用Cat2Doc')


